﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using COFE.Core.Lister;
using COFE.Core;
using System.IO.IsolatedStorage;

namespace COFE.IO.ISG
{
    public class IsolatedStorageDirectoryController : DirectoryControllerBase<ISGEntryInfo>
    {
        #region Constructor

        //private void copyDirectory(DirectoryInfo sourceDir, DirectoryInfo targetDir)
        //{
        //    DirectoryInfo targetSourceDir = targetDir.CreateSubdirectory(sourceDir.Name);
        //    foreach (var entries in targetDir.GetFileSystemInfos())
        //    {
        //        string destName = Path.Combine(targetSourceDir.FullName, entries.Name);
        //        if (entries is FileInfo)
        //            File.Copy(entries.FullName, destName);
        //        else copyDirectory(entries as DirectoryInfo, targetSourceDir);
        //    }
        //}

        public IsolatedStorageDirectoryController(IsolatedStorageDirectoryLister directoryLister)
            : base(directoryLister)
        {



            _createDirectory = (parent, name) => 
            { 
                var dir = new ISGEntryInfo(directoryLister.IsolatedStorageFile, Path.Combine(parent.RelativePath, name), true);
                directoryLister.IsolatedStorageFile.CreateDirectory(dir.RelativePath);
                return dir;                
            };
            _createFile = (parent, name) =>
            {
                var file = new ISGEntryInfo(directoryLister.IsolatedStorageFile, Path.Combine(parent.RelativePath, name), false);
                using (var ifs = new IsolatedStorageFileStream(file.RelativePath, FileMode.Create, FileAccess.Write, file.StorageFile))
                    ifs.Flush();                    
                return file;
            };

            //_copy = (item, taretDir) =>
            //{
            //    if (item is FileInfoEx) File.Copy(item.FullName, Path.Combine(taretDir.FullName, item.Name));
            //    else
            //        DirectoryEx.Copy((item as DirectoryInfoEx).FullName, (taretDir as DirectoryInfoEx).FullName);
            //        //copyDirectory(item as DirectoryInfoEx, taretDir as DirectoryInfoEx);
            //};
            //_move = (item, taretDir) =>
            //{
            //    string destName = PathEx.Combine(taretDir.FullName, item.Name);
            //    if (item is FileInfoEx) FileEx.Move(item.FullName, destName);
            //    else DirectoryEx.Move(item.FullName, destName);
            //};

            //_rename = (item, newName) =>
            //{
            //    string source = item.FullName;
            //    string dest = Path.Combine(Path.GetDirectoryName(item.FullName), newName);
            //    if (File.Exists(source))
            //        File.Move(source, dest);
            //    else Directory.Move(source, dest);
            //};

            _delete = (item) =>
            {
                if (item.IsFolder)
                {
                    removeFolder(directoryLister.IsolatedStorageFile, item.RelativePath);
                    //directoryLister.IsolatedStorageFile.DeleteDirectory(item.RelativePath);
                }
                else directoryLister.IsolatedStorageFile.DeleteFile(item.RelativePath);                
            };

        }

        #endregion

        #region Methods

        private static void removeFolder(IsolatedStorageFile storageFile, string directory)
        {
            try
            {
                foreach (var file in storageFile.GetFileNames(directory + "\\*"))
                    storageFile.DeleteFile(COFEPath.Combine(directory, file));

                foreach (var dir in storageFile.GetDirectoryNames(directory + "\\*"))
                    removeFolder(storageFile, COFEPath.Combine(directory, dir));

                storageFile.DeleteDirectory(directory);
            }
            catch { }
        }

        #endregion

        #region Data

        #endregion

        #region Public Properties

        #endregion
    }
}
